package firbi.base;

import android.database.Cursor;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.SmallTest;


/**
 * This tests that favorites are properly added and deleted from the database.  It does not allow duplicate favorites to be
 * placed into the database and does not allow phantom/nonexistent favorites to be removed.
 * @author jenn
 *
 */


public class FavoritesDbAdapter_Tests extends ActivityInstrumentationTestCase2<Favorites> {
	private Favorites activity;
	
	
	public FavoritesDbAdapter_Tests() {
		super("firbi.base", Favorites.class);
	}
	
	
	@Override
    protected void setUp() throws Exception {
        super.setUp();
		activity = getActivity();
    }
	
	
	
	
	
	
	
	//test that adding a new favorite to the database actually adds the favorite and gives a successful string return
	@SmallTest
	public void testAddingFavorite(){
		//vars
		 String ROUTE_ID_1 = "0001_83";
		 String STOP_ID_1 = "001_10220";
		 String ROUTE_ID_2 ="0001_71";
		 String STOP_ID_2 ="0001_880";
		
		//clear the database so the favorite isn't in the db
		activity.mFavDbHelper.deleteAllFavs();
		//put it into the database
	long result = activity.mFavDbHelper.createFav("x", "y", ROUTE_ID_1, STOP_ID_1);
		
		//make sure the print out says it was successfully put in
		assertTrue("Should have been added the entry from the database", -1< result);
		
		//make sure it's in the database
		assertTrue("the favorite shoule be in the database now", activity.mFavDbHelper.checkIfFav(ROUTE_ID_1, STOP_ID_1));
	}
	
	
	
	
	
	
	
	//test that the program successfully deletes a favorite from the database
	@SmallTest
	public void testDeletingFavorite(){
		//vars
		 String ROUTE_ID_1 = "0001_83";
		 String STOP_ID_1 = "001_10220";
		 String ROUTE_ID_2 ="0001_71";
		 String STOP_ID_2 ="0001_880";
		
		//make sure the element is in the database so remove everything and explicitly add it
		activity.mFavDbHelper.deleteAllFavs();
		long row_id = activity.mFavDbHelper.createFav("r","r", ROUTE_ID_1, STOP_ID_1);

		//delete it from the database
		boolean result = activity.mFavDbHelper.deleteFav(row_id);
		
		//make sure the string result is correct
		assertTrue("Should have deleted the entry from the database so delete should be true", result);
		
		//make sure it's in the database
		assertFalse("favorite shouldn't be in the database now so checking should show that it's not there", activity.mFavDbHelper.checkIfFav(ROUTE_ID_1, STOP_ID_1));
	}
	

	
	
	@SuppressWarnings("static-access")
	@SmallTest
	public void testAddingPreExistantFav(){
		//vars
		 String ROUTE_ID_1 = "0001_83";
		 String STOP_ID_1 = "001_10220";
		 String ROUTE_ID_2 ="0001_71";
		 String STOP_ID_2 ="0001_880";
		 
		//make sure fav is already in db before trying to add it again
	    activity.mFavDbHelper.createFav("t","t",ROUTE_ID_1, STOP_ID_1);
		
		//try adding it again
		long result = activity.mFavDbHelper.createFav("t","t",ROUTE_ID_1, STOP_ID_1);
		assertEquals("Shouldn't be allowed to add same favorite multiple times", activity.mFavDbHelper.FAV_ALREADY_IN_DB, result);
	}
	
	
	
	
	
	
	@SmallTest
	public void testDeletingNonExistantFav(){
		//vars
		 String ROUTE_ID_1 = "0001_83";
		 String STOP_ID_1 = "001_10220";
		 String ROUTE_ID_2 ="0001_71";
		 String STOP_ID_2 ="0001_880";
		 
		//make sure fav is not in db
		//clear the database so the favorite isn't in the db
		activity.mFavDbHelper.deleteAllFavs();	
		
		//try to delete nonexistent fav, since the database is empty and row id will work
		boolean result = activity.mFavDbHelper.deleteFav(1);
		
		//check to see that it wasn't 'deleted' b/c it couldn't have been
		assertFalse("Shouldn't be able to delete a favorite that does not exist in the database", result);
	}
	
	
	@SuppressWarnings("static-access")
	@SmallTest
	public void testDeleteAll(){
		//vars
		 String ROUTE_ID_1 = "0001_83";
		 String STOP_ID_1 = "001_10220";
		 String ROUTE_ID_2 ="0001_71";
		 String STOP_ID_2 ="0001_880";
		 
		//add something so the database isn't empty
		if(!activity.mFavDbHelper.checkIfFav(ROUTE_ID_1, STOP_ID_1))
			activity.mFavDbHelper.createFav("t","t",ROUTE_ID_1, STOP_ID_1);
		
		//now make sure that the database has something in it
		Cursor mCursor = activity.mFavDbHelper.mFavsDb.query(true, FavsDbAdapter.DB_TABLE, new String[] {activity.mFavDbHelper.KEY_ROWID}, null, null, null, null, null, null);
		assertTrue("there should be something in the database here", mCursor.getCount() != 0);
          
		//delete everything
		activity.mFavDbHelper.deleteAllFavs();
		
		//make sure the database has nothing in it
		mCursor = null;
		mCursor = activity.mFavDbHelper.mFavsDb.query(true, FavsDbAdapter.DB_TABLE, new String[] {activity.mFavDbHelper.KEY_ROWID}, activity.mFavDbHelper.KEY_COUNT + "= 50", null, null, null, null, null);
		assertEquals("there should be nothing in the database here", 0, mCursor.getCount());
	}

}